<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage Web
 * @version $Id$
 */

/**	
 * The Filter Chain 
 *	
 * A FilterChain is an object provided by the framework to the developer giving 
 * a view into the invocation chain of a filtered request. Filters use the FilterChain to 
 * invoke the next filter in the chain, or if the calling filter is the 
 * last filter in the chain, to invoke the resource at the end of the chain. 
 * 
 * @package auxo
 * @subpackage Web
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class tx_auxo_FilterChain {
	
	/**
	 * An array of filter instances
	 *
	 * @var array 
	 */
	protected $filters = array();
	
	/**
	 * A Configuration Instance
	 *
	 * @var tx_auxo_Cofiguration
	 */
	protected $configuration = NULL;
	
	/**
	 * Creates a new filter chain with an array of filter instances
	 *
	 * @param array $filters array of filter instances
	 */
	public function __construct(tx_auxo_Configuration $configuration, array $filters) {
		$this->configuration = $configuration;
		$this->setFilters($filters);
	}
	
	/**
	 * Registers a filter instance
	 *
	 * @param tx_auxo_FilterInterface $filter
	 */
	public function	setFilter(tx_auxo_FilterInterface $filter) {
		$filter->initialize($this->configuration);
		$this->filters[] = $filter;
	}
	
	/**
	 * Sets an array of filters for this filter chain
	 *
	 * @param array $filters an array of filter instances
	 */
	public function setFilters(array $filters) {
		foreach($filters as $filter) {
			$this->setFilter($filter);
		}
	}
	
	/**
	 * Causes the next filter in the chain to be invoked, or if the calling filter is the 
	 * last filter in the chain, causes the resource at the end of the chain to be invoked.
	 *
	 * @param tx_auxo_Request $request
	 * @param tx_auxo_Response $response
	 */
	public function invoke(tx_auxo_Request $request, tx_auxo_Response $response) {
		if (($filter = current($this->filters))) {
			next($this->filters);
			$filter->filter($request, $response, $this);
		}
	}
}
?>